package com.jz.community.moduleshopping.orderDetail.ui;

import android.app.Activity;
import android.content.Context;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.orderDetail.bean.OrderDetailBean;
import com.jz.community.moduleshopping.orderDetail.bean.OrderGoodsItemInfo;
import com.jz.community.moduleshopping.orderDetail.task.OrderGoodsItemTask;
import com.qmuiteam.qmui.widget.QMUIFloatLayout;

import java.util.List;

/**
 * Created by Tony.Fan on 2018/5/28 14:36
 * 取消订单弹框
 */
public class CancelCouponOrderPop implements View.OnClickListener {
    private Context mContext;
    private TextView closeIv, cancel_order_tv;
    private View view;
    private BottomSheetDialog bottomSheetDialog;
    private QMUIFloatLayout order_good_name_layout;

    private ReceiveCouponListener receiveCouponListener;

    public void setReceiveCouponListener(ReceiveCouponListener receiveCouponListener) {
        this.receiveCouponListener = receiveCouponListener;
    }

    public CancelCouponOrderPop(Context context) {
        mContext = context;
        initView();
        addListener();
    }

    public void addOrderId(String orderId) {
        getOrderGoodsInfo(orderId);
        showPopUp();
    }

    private void getOrderGoodsInfo(String orderId){
        new OrderGoodsItemTask((Activity) mContext, new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {
                OrderGoodsItemInfo orderGoodsItemInfo = (OrderGoodsItemInfo)o;
                if (!Preconditions.isNullOrEmpty(orderGoodsItemInfo)){
                    showGoodsNameTv(orderGoodsItemInfo.get_embedded().getOrderInfoList());
                }
            }
        }).execute(orderId);
    }

    private void addListener() {
        closeIv.setOnClickListener(this);
        cancel_order_tv.setOnClickListener(this);
    }

    public void showPopUp() {
        if (bottomSheetDialog == null) {
            bottomSheetDialog = new BottomSheetDialog(mContext);
            bottomSheetDialog.setContentView(view);
        }
        bottomSheetDialog.show();
    }

    public void initView() {
        view = LayoutInflater.from(mContext).inflate(R.layout.order_detail_cancel_pop_layout, null);
        order_good_name_layout = view.findViewById(R.id.order_good_name_layout);
        closeIv = view.findViewById(R.id.close_pop_tv);
        cancel_order_tv = view.findViewById(R.id.cancel_order_tv);
    }

    /**
     * 显示商品名称
     *
     * @param orderItemListBeans
     */
    private void showGoodsNameTv(List<OrderGoodsItemInfo.EmbeddedBean.OrderInfoListBean> orderItemListBeans) {
        for (OrderGoodsItemInfo.EmbeddedBean.OrderInfoListBean orderItem : orderItemListBeans) {
            for (OrderGoodsItemInfo.EmbeddedBean.OrderInfoListBean.OrderItemListBean orderItemListBean:orderItem.getOrderItemList()){
                order_good_name_layout.addView(createTextView(orderItemListBean.getGoodsName()));
            }
        }
    }

    /**
     * 创建Textview
     *
     * @param btnName
     * @return
     */
    private TextView createTextView(String btnName) {
        ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.leftMargin = SHelper.dp2px(mContext, 5);
        lp.rightMargin = SHelper.dp2px(mContext, 5);
        TextView textView = new TextView(mContext);
        textView.setTextColor(ContextCompat.getColor(mContext, R.color.balck));
        textView.setPadding(SHelper.dp2px(mContext, 5), 0, SHelper.dp2px(mContext, 5), 0);
        textView.setLayoutParams(lp);
        textView.setTextSize(14);
        textView.setText(btnName);
        return textView;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.close_pop_tv) {
            bottomSheetDialog.dismiss();
        }
        if (v.getId() == R.id.cancel_order_tv) {
            receiveCouponListener.receiveCouponSuccess();
            bottomSheetDialog.dismiss();
        }
    }

    //领劵成功回调
    public interface ReceiveCouponListener {
        void receiveCouponSuccess();
    }

}
